package org.glut.wechat.moments.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glut.wechat.moments.bo.User;
import org.glut.wechat.moments.domain.MomentsNotice;
import org.glut.wechat.moments.feign.PersonalCenterFeignService;
import org.glut.wechat.moments.mapper.MomentsNoticeMapper;
import org.glut.wechat.moments.service.MomentsNoticeService;
import org.glut.wechat.moments.vo.MomentNoticeItemVO;
import org.glut.wechat.moments.vo.UnreadCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 朋友圈消息通知服务实现类
 */
@Service
public class MomentsNoticeServiceImpl extends ServiceImpl<MomentsNoticeMapper, MomentsNotice> implements MomentsNoticeService {

    @Autowired
    private PersonalCenterFeignService personalCenterFeignService;


    @Override
    public IPage<MomentNoticeItemVO> getNoticesByNoticeUserId(Page<MomentNoticeItemVO> page, Integer noticeUserId) {
        markAllAsRead(noticeUserId);
       return baseMapper.getNoticesByNoticeUserId(page, noticeUserId);
    }


    @Override
    public void markAllAsRead(Integer noticeUserId) {
        UpdateWrapper<MomentsNotice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("notice_user_id", noticeUserId)
                .set("is_read", 1)
                .eq("is_read", 0);
        baseMapper.update(updateWrapper);
    }

    @Override
    public Integer countUnreadNotices(Integer noticeUserId) {
        QueryWrapper<MomentsNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_user_id", noticeUserId)
                .eq("is_read", 0);
        return baseMapper.selectCount(queryWrapper).intValue();
    }

    @Override
    public void deleteNotice(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public UnreadCountVO noticeRead(Integer noticeUserId) {

        UnreadCountVO unreadCountVO = new UnreadCountVO();
        QueryWrapper<MomentsNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_user_id", noticeUserId).eq("is_read", 0);
        queryWrapper.orderByDesc("create_time");
        List<MomentsNotice> momentsNotices = baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(momentsNotices)){
            unreadCountVO.setCount(0);
            unreadCountVO.setAvatar(null);
        }else {
            unreadCountVO.setCount(momentsNotices.size());
            ResponseEntity<User> userInfo = personalCenterFeignService.getUserInfo(momentsNotices.get(0).getOperationUserId());
            User body = userInfo.getBody();
            unreadCountVO.setAvatar(body!=null?body.getAvatar():"");
        }
        return unreadCountVO;
    }

}
